package com.atguigu.spzx.model.common;

import lombok.Data;

/**
 * title:
 * author: zgl
 * date: 2023/12/26
 * description：
 */
@Data
public class Result<T> {
    private Integer code;
    private String message;
    private T data;

    //快速创建result对象
    public static <T> Result setResult(Integer code, String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> Result setResult(ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<>();
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        result.setData(null);
        return result;
    }

    //成功
    public static <T> Result ok() {
        return setResult(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), null);
    }

    public static <T> Result ok(T data) {
        return setResult(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), data);
    }

    //失败
    public static <T> Result fail() {
        return setResult(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getMessage(), null);
    }


    //方便链式调用
    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }

    public Result message(String message) {
        this.setMessage(message);
        return this;
    }
}
